package com.yang;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yang.utils.MyImageView;
import com.yang.utils.UtilsText;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView city, weather, temperature;
    private MyImageView imageView;
    private String BASE_URL ="http://172.28.34.164:9090/weather/get?id=";
    private String IMAGE_URL ="http://172.28.34.164:9090/";
    private GestureDetector gestureDetector;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        city = findViewById(R.id.city);
        weather = findViewById(R.id.weather);
        temperature = findViewById(R.id.temperature);
        imageView = findViewById(R.id.imageView);
        getWeather(0);
        gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (e1.getRawX() - e2.getRawX() > 200) {
                    index ++;
                    getWeather(index % 4);
                }
                if (e2.getRawX() - e1.getRawX() > 200) {
                    index --;
                    if (index < 0) {
                        index = index % 4 + 4;
                    }
                    getWeather(index % 4);
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });
    }

    private void getWeather(final int id) {
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url = new URL(BASE_URL + id);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.connect();
                    Message message = new Message();
                    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        message.what = 200;
                        InputStream inputStream = connection.getInputStream();
                        String str = UtilsText.change(inputStream);
                        message.obj = str;
                        handler.sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 200) {
                Gson gson = new Gson();
                Weather getWeather = gson.fromJson(String.valueOf(msg.obj), Weather.class);
                city.setText(getWeather.getCity());
                temperature.setText(getWeather.getTemperature() + "℃");
                weather.setText(getWeather.getWeather());
                imageView.setImageURL(IMAGE_URL + getWeather.getImg());
            }
        }
    };

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
}
